package com.xiaoyi.intentsdklibrary.Bean;

import android.content.Context;
import com.xiaoyi.intentsdklibrary.Bean.DaoMaster;
import com.xiaoyi.intentsdklibrary.Bean.PhoneBeanDao;
import java.util.ArrayList;
import java.util.List;
import org.greenrobot.greendao.query.WhereCondition;

public class PhoneBeanSqlUtil {
    private static final PhoneBeanSqlUtil ourInstance = new PhoneBeanSqlUtil();
    private PhoneBeanDao mPhoneBeanDao;

    public static PhoneBeanSqlUtil getInstance() {
        return ourInstance;
    }

    private PhoneBeanSqlUtil() {
    }

    public void initDbHelp(Context context) {
        this.mPhoneBeanDao = new DaoMaster(new DaoMaster.DevOpenHelper(context, "intentsdk-db", null).getWritableDatabase()).newSession().getPhoneBeanDao();
    }

    public void add(PhoneBean phoneBean) {
        this.mPhoneBeanDao.insertOrReplace(phoneBean);
    }

    public void addList(List<PhoneBean> list) {
        this.mPhoneBeanDao.insertOrReplaceInTx(list);
    }

    public void update(PhoneBean phoneBean) {
        this.mPhoneBeanDao.update(phoneBean);
    }

    public void delByID(String str) {
        if (str == null) {
            str = "";
        }
        ArrayList arrayList = (ArrayList) this.mPhoneBeanDao.queryBuilder().where(PhoneBeanDao.Properties.PhoneNum.eq(str), new WhereCondition[0]).list();
        if (arrayList.size() > 0) {
            this.mPhoneBeanDao.deleteInTx(arrayList);
        }
    }

    public void delAll() {
        try {
            this.mPhoneBeanDao.deleteInTx(this.mPhoneBeanDao.queryBuilder().build().list());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<PhoneBean> searchByID(String str) {
        Exception e;
        ArrayList arrayList = new ArrayList();
        if (str == null) {
            str = "";
        }
        try {
            ArrayList arrayList2 = (ArrayList) this.mPhoneBeanDao.queryBuilder().where(PhoneBeanDao.Properties.PhoneNum.eq(str), new WhereCondition[0]).list();
            if (arrayList2 != null) {
                return arrayList2;
            }
            try {
                return new ArrayList();
            } catch (Exception e2) {
                arrayList = arrayList2;
                e = e2;
                try {
                    e.printStackTrace();
                    return arrayList;
                } catch (Throwable unused) {
                    return arrayList;
                }
            } catch (Throwable unused2) {
                return arrayList2;
            }
        } catch (Exception e3) {
            e = e3;
            e.printStackTrace();
            return arrayList;
        }
    }

    public List<PhoneBean> searchAll() {
        List<PhoneBean> list = this.mPhoneBeanDao.queryBuilder().build().list();
        return list == null ? new ArrayList() : list;
    }
}
